﻿using System;
using System.Linq;
using System.Collections.ObjectModel;
using CheckLift.Common.Intf;
using System.Data.Linq;

namespace CheckLift.Linq
{
    public class CompanyData : BaseData
    {
        public CompanyData()
        {
            DataLoadOptions options = new DataLoadOptions();

            options.LoadWith<Company>(c => c.Address);
            dc.LoadOptions = options;
        }

        public ObservableCollection<ICompany> GetCompaniesByUserId(Guid userId, Guid companyId)
        {
            if (companyId == Guid.Empty)
            {
                return Convert<ICompany>(from company in dc.Companies
                                        join company_link in dc.User_Company_Links on company.Id equals company_link.CompanyId
                                        where company_link.UserId == userId
                                        select company);
            }
            else
            {
                ObservableCollection<ICompany> lstComp = new ObservableCollection<ICompany>();
                lstComp.Add(dc.Companies.SingleOrDefault<Company>(c => c.Id == companyId));
                return lstComp;
                //return Convert<ICompany>(from company in dc.Companies
                //                        join company_link in dc.User_Company_Links on company.Id equals company_link.CompanyId
                //                        where company_link.UserId == userId && company.Id == companyId
                //                        select company);
            }           
        }
    }
}
